package com.csdj.t309springboot.controller;
import com.csdj.t309springboot.common.CacheConstant;
import com.csdj.t309springboot.common.WebResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 基本控制器
 */
public class BaseController {
    private Logger log = LoggerFactory.getLogger("");
    /**
     * 获取请求头信息中的用户编号
     * @param request http请求
     * @return
     */
    public int getUid(HttpServletRequest request){
        //根据请求对象获取头信息, 参数是 uid
        String header = request.getHeader(CacheConstant.HEADER_UID);
        //如果头信息中存在则转换为整型
        if(StringUtils.hasText(header)){
            return Integer.parseInt(header);
        }
        return 0;
    }


    /**
     * 获取请求头信息中的拼图详情编号
     * @param request http请求
     * @return
     */
//    @ResponseBody
//    @GetMapping("/getJigsawPuzzleId")
    public int getJigsawPuzzleId(HttpServletRequest request){
        log.info("request.getHeader(\"jigsawPuzzleId\")="+request.getHeader(CacheConstant.HEADER_JIGSAW_PUZZLE_ID));
        //根据请求对象获取头信息, 参数是 uid
        String header = request.getHeader(CacheConstant.HEADER_JIGSAW_PUZZLE_ID);
        //如果头信息中存在则转换为整型
        if(StringUtils.hasText(header)){
            return Integer.parseInt(header);
        }
        return 0;
    }

    public WebResult resp(int code, Object data){
        return new WebResult(code,data);
    }

}
